/*
* @Author: Choisaaaa
* @Date: 2018/5/8  22:49
* 用户列表搜索时的下拉框 组件
*/

import React from "react";
class ListSearch extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            searchType : "userId",
            searchKeyWord : ""
        }
    }
    onValueChange(e){
        let name = e.target.name;
        let value = e.target.value.trim();
        this.setState({
            [name] : value
        });
        //用户类型 不需要从输入框获取searchKeyWord值，所以这里自动赋值。赋值完成直接调用父组件的onSearch（）方法，重新渲染
        if(value =="status-0"){//查找系统中已经拉黑的用户
            this.setState({
                searchType : "status",
                searchKeyWord : 0
            },()=>{
                this.onSearch();
            });
        }else if(value =="status-1"){//查找系统中正常的用户
            this.setState({
                searchType : "status",
                searchKeyWord : 1
            },()=>{
                this.onSearch();
            });
        }
    }
    onSearch(){
        this.props.onSearch(this.state.searchType,this.state.searchKeyWord);
    }
    onSearchKeywordKeyUp(e){
        if(e.keyCode === 13){
            this.onSearch()
        }
    }
    render(){
        return(
            <div className="row search-wrap">
                <div className="col-md-12">
                    <div className="form-inline">
                        <div className="form-group">
                            <select className="form-control"
                                    name="searchType"
                                    onChange={(e) => this.onValueChange(e)}>
                                <option value="userId">按用户的ID查询</option>
                                <option value="userName">按用户名称查询</option>
                                <option value="status-0">检索拉黑的用户</option>
                                <option value="status-1">正常使用的用户</option>
                            </select>
                        </div>
                        <div className="form-group">
                            <input type="text"
                                   className="form-control"
                                   placeholder="请输入关键词.."
                                   name="searchKeyWord"
                                   onKeyUp={(e) => this.onSearchKeywordKeyUp(e)}
                                   onChange={(e) => this.onValueChange(e)}/>
                        </div>
                        <button  className="btn btn-primary" onClick={(e) => this.onSearch()}> <i className="fa fa-search"/> Search</button>
                    </div>
                </div>
            </div>
        );
    }
}
export default ListSearch;